package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.LiveAdapter;
import im.starchat.chat.control.QavsdkControl;
import im.starchat.chat.dialog.ShareRoomDialogFragment;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.RoomBean;
import im.starchat.chat.utils.Constants;
import im.starchat.chat.utils.Util;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by liuchao on 2/3/16.
 * 直播 fragment
 */
public class StarLiveFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    private static final String TAG = "LiveFragment";

    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private QavsdkControl mQavsdkControl;
    private LiveAdapter mLiveAdapter;
    private LinearLayoutManager mLayoutManager;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    private static final String STAR_ID = "star_id";

    public StarLiveFragment() {
    }

    public static StarLiveFragment newInstance(String star_id) {
        StarLiveFragment fragment = new StarLiveFragment();
        Bundle bundle = new Bundle();
        bundle.putString( STAR_ID, star_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_live, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mLiveAdapter = new LiveAdapter(getActivity(), new LiveAdapter.LiveAdapterCallback() {
            @Override
            public void onItemClick(RoomBean feedBean) {
//                if (feedBean != null && feedBean.getUser().getId() == (UserUtils.getUserInstance().getId())) {
//                    Toast.makeText(getActivity(), "你不能加入自己的房间", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                Log.d(TAG, "onItemClick isCreater false");
                Constants.setChooseRoomInfo(feedBean);
                createRoom(feedBean.getId());
            }

            @Override
            public void onShareBtnClick(RoomBean feedBean) {
                final ShareRoomDialogFragment shareRoomDialogFragment = ShareRoomDialogFragment.newInstance(feedBean);
                shareRoomDialogFragment.show(getChildFragmentManager().beginTransaction(), "share_app");
            }
        });
        mRecyclerView.setAdapter(mLiveAdapter);
        mQavsdkControl = XLApplication.getQavsdkControl();

        getFeedList();

//        getLiveVideoInfo();

        return view;
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getFeedList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getFeedList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(getActivity(), "没有更多数据", Toast.LENGTH_SHORT).show();
        }

    }

    private void getFeedList(){
        XLApplication.getInstance().getRestClient().getUserRooms(getArguments().getString(STAR_ID), mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<RoomBean[]>() {
            @Override
            public void success(RoomBean[] feedBeans, Response response) {
                if(mCurrentPage == RestClient.DEFAULT_PAGE){
                    mLiveAdapter.refreshData(new ArrayList<RoomBean>(Arrays.asList(feedBeans)));
                }else{
                    mLiveAdapter.addAll(new ArrayList<RoomBean>(Arrays.asList(feedBeans)));
                }
                if(feedBeans.length < 20){
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }

    private void createRoom(int num) {
        if (Util.isNetworkAvailable(getActivity().getApplicationContext())) {
            if (num != 0) {
                int nums = num;
                mQavsdkControl.enterRoom(nums);
                Toast.makeText(getActivity(), "正在进入直播中...", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getActivity(), getString(R.string.notify_no_network), Toast.LENGTH_SHORT).show();
        }
    }

}
